package com.ruoyi.information.service.pc.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.information.domain.pc.CompanyNeed;
import com.ruoyi.information.domain.pc.SysUser;
import com.ruoyi.information.mapper.pc.CompanyNeedMapper;
import com.ruoyi.information.mapper.pc.SysUserMapper;
import com.ruoyi.information.service.pc.ICompanyNeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 企业需求Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-17
 */
@Service
public class CompanyNeedServiceImpl implements ICompanyNeedService {
    @Autowired
    private CompanyNeedMapper companyNeedMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询企业需求
     *
     * @param id 企业需求主键
     * @return 企业需求
     */
    @Override
    public CompanyNeed selectCompanyNeedById(Long id) {
        CompanyNeed need = companyNeedMapper.selectCompanyNeedById(id);
        SysUser user = sysUserMapper.selectSysUserByUserId(need.getUserId());
        if (user != null && user.getNickName() != null) {
            need.setUsername(user.getNickName());
        }
        /*增加浏览量*/
        companyNeedMapper.updateNum(id);
        return need;
    }

    /**
     * 查询企业需求列表
     *
     * @param companyNeed 企业需求
     * @return 企业需求
     */
    @Override
    public List<CompanyNeed> selectCompanyNeedList(CompanyNeed companyNeed) {
        List<CompanyNeed> list = companyNeedMapper.selectCompanyNeedList(companyNeed);
        for (CompanyNeed need : list) {
            SysUser user = sysUserMapper.selectSysUserByUserId(need.getUserId());
            if (user != null && user.getNickName() != null) {
                need.setUsername(user.getNickName());
            }
        }
        return list;
    }

    /**
     * 新增企业需求
     *
     * @param companyNeed 企业需求
     * @return 结果
     */
    @Override
    public int insertCompanyNeed(CompanyNeed companyNeed) {
        companyNeed.setCreateTime(DateUtils.getNowDate());
        return companyNeedMapper.insertCompanyNeed(companyNeed);
    }

    /**
     * 修改企业需求
     *
     * @param companyNeed 企业需求
     * @return 结果
     */
    @Override
    public int updateCompanyNeed(CompanyNeed companyNeed) {
        companyNeed.setUpdateTime(DateUtils.getNowDate());
        return companyNeedMapper.updateCompanyNeed(companyNeed);
    }

    /**
     * 批量删除企业需求
     *
     * @param ids 需要删除的企业需求主键
     * @return 结果
     */
    @Override
    public int deleteCompanyNeedByIds(Long[] ids) {
        return companyNeedMapper.deleteCompanyNeedByIds(ids);
    }

    /**
     * 删除企业需求信息
     *
     * @param id 企业需求主键
     * @return 结果
     */
    @Override
    public int deleteCompanyNeedById(Long id) {
        return companyNeedMapper.deleteCompanyNeedById(id);
    }
}
